package com.xr.activiti;

import com.xr.activiti.pojo.Evection;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;

class ActivitiDemo2
{
    @Test
    public void deploymentTest()
    {
        //  1.创建ProcessEngine
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //  2.获取repositoryService
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        //  3.使用该service进行流程部署，定义一个流程名字，把bpmn和png部署到数据中
        Deployment deployment = repositoryService.createDeployment().name("出差申请-并行网关")
                .addClasspathResource("bpmn/evection-parallel.bpmn20.xml")
                .deploy();
        //  4.输出部署信息
        System.out.println("流程部署id:"+deployment.getId());
        System.out.println("流程部署名字:"+deployment.getName());
    }

    @Test
    public void startAssigneeUel()
    {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        HashMap<String, Object> assigneeMap = new HashMap<>();
        //  这个会先于MyTaskListener生效
        assigneeMap.put("assignee0","李四");
        assigneeMap.put("assignee1","李经理");
        assigneeMap.put("assignee2","王总经理");
        assigneeMap.put("assignee3","赵财务");

        runtimeService.startProcessInstanceByKey("evection-uel","10001",assigneeMap);
    }


    @Test
    public void startProcessTest()
    {
        //  1.创建processEngine
        //  2.获取RunTimeService
        //  3.根据流程定义id启动流程
        //  4.获取输出内容
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();

        ProcessInstance evection = runtimeService.
                startProcessInstanceByKey("evection-parallel","10001");
        System.out.println("流程定义id：" + evection.getProcessDefinitionId());
        System.out.println("流程实例id：" + evection.getId());
        System.out.println("当前活动Id：" + evection.getActivityId());
    }

    @Test
    public void startAssigneeGlobal()
    {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();

        Evection evection = new Evection();
        evection.setNum(3);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("evection",evection);

        //  这个会先于MyTaskListener生效
        hashMap.put("assignee0","李四");
        hashMap.put("assignee1","李经理");
        hashMap.put("assignee2","王总经理");
        hashMap.put("assignee3","赵财务");

        runtimeService.startProcessInstanceByKey("evection-global","10001",hashMap);
    }

    /**
     * 通过流程实例id设置全局变量，该流程实例必须未执行完成。
     */
    @Test
    public void setGlobalVariableByExecutionId()
    {
//    当前流程实例执行id，通常设置为当前执行的流程实例
        String executionId="107502";
//     获取processEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        获取RuntimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();
//        创建出差pojo对象
        Evection evection = new Evection();
//        设置天数
        evection.setNum(10d);
//      通过流程实例 id设置流程变量
        runtimeService.setVariable(executionId, "evection", evection);
//      一次设置多个值
//      runtimeService.setVariables(executionId, variables)
    }


    @Test
    public void suspendAllProcessInstance()
    {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        ProcessDefinition processDefinition = processDefinitionQuery.processDefinitionKey("evection").singleResult();

        boolean suspended = processDefinition.isSuspended();
        if (suspended)
        {
            // 如果是暂停，可以执行激活操作 ,参数1 ：流程定义id ，参数2：是否激活，参数3：激活时间
            repositoryService.activateProcessDefinitionById(processDefinition.getId(),true,null);
            System.out.println("流程定义："+processDefinition.getId()+",已激活");
        }else
        {
            repositoryService.suspendProcessDefinitionById(processDefinition.getId(),true,null);
            System.out.println("流程定义："+processDefinition.getId()+",已挂起");
        }
    }

    @Test
    public void suspendSingleProcessInstance()
    {
        //  首先获取流程实例id
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();
        Task task = taskService.createTaskQuery().processDefinitionKey("evection").
                taskAssignee("zhangsan").singleResult();
        String processInstanceId = task.getProcessInstanceId();


        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();
        ProcessInstance processInstance = processInstanceQuery.processInstanceId(processInstanceId).singleResult();

        boolean suspended = processInstance.isSuspended();
        if (suspended)
        {
            // 如果是暂停，可以执行激活操作 ,参数1 ：流程定义id ，参数2：是否激活，参数3：激活时间
            runtimeService.activateProcessInstanceById(processInstance.getId());
            System.out.println("流程实例："+processInstance.getId()+",已激活");
        }else
        {
            runtimeService.suspendProcessInstanceById(processInstance.getId());
            System.out.println("流程实例："+processInstance.getId()+",已挂起");
        }
    }

    @Test
    void contextLoads()
    {
        //  使用activiti提供的工具类来创建mysql表
        //  默认从resource/activiti.cfg.xml下读取
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
//        System.out.println(defaultProcessEngine);

        //  一般方式
        ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.
                createProcessEngineConfigurationFromResource("activiti.cfg.xml","processEngineConfiguration");
        ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();
    }


    /**
     * 候选人拾取任务
     */
    @Test
    public void claimTask()
    {
        //  获取processEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();

        //要拾取的任务id
        String taskId = "97502";
        //任务候选人id
        String candidateUser = "lisi";
        //拾取任务
        //即使该用户不是候选人也能拾取(建议拾取时校验是否有资格)
        //校验该用户有没有拾取任务的资格
        Task task = taskService.createTaskQuery()
                .taskId(taskId)
                .taskCandidateUser(candidateUser)//根据候选人查询
                .singleResult();
        if(task != null)
        {
            //拾取任务
            taskService.claim(taskId, candidateUser);
            System.out.println("任务拾取成功");
        }
    }
}
